package vn.personal.anhht.bloback12.database;




import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class CallBlockDatabaseFilterAdapter {
	
	
	public static final String KEY_ID = "id";
	public static final String KEY_WORD = "word";
	
	public static final String TABLE_FILTER = "filters";
	
	
	private Context context;
	private SQLiteDatabase database;
	private CallBlockDatabaseHelper dbHelper;
	
	
	public CallBlockDatabaseFilterAdapter(){
		this.context = null;
	}
	
	public CallBlockDatabaseFilterAdapter(Context _context){
		this.context = _context;
	}
	
	public CallBlockDatabaseFilterAdapter open() throws SQLException {
		dbHelper = new CallBlockDatabaseHelper(context);
		database = dbHelper.getWritableDatabase();
		return this;
	}
	
	public void close(){
		dbHelper.close();
	}

	public android.database.Cursor selectAllWords(){
		String[] columns = {KEY_ID, KEY_WORD};
		String selection = null;
		String[] selectionAgrs = null;
		String groupBy = null;
		String having = null;
		String orderBy = KEY_WORD;
		
		return this.database.query(TABLE_FILTER, columns, selection, selectionAgrs, groupBy, having, orderBy);
	}

	public android.database.Cursor selectFilterById(int _id){
		String[] columns = {KEY_ID, KEY_WORD};
		String selection = KEY_ID + " = " + _id;
		String[] selectionAgrs = null;
		String groupBy = null;
		String having = null;
		String orderBy = null;
		
		return this.database.query(TABLE_FILTER, columns, selection, selectionAgrs, groupBy, having, orderBy);
	}
	
	public android.database.Cursor selectFilterByWord(String word){
		String[] columns = {KEY_ID, KEY_WORD};
		String selection = KEY_WORD + " = '" + word.trim() + "'";
		String[] selectionAgrs = null;
		String groupBy = null;
		String having = null;
		String orderBy = null;
		return this.database.query(TABLE_FILTER, columns, selection, selectionAgrs, groupBy, having, orderBy);
	}
	
	private ContentValues initParameters(
			String word){
		ContentValues parameters = new ContentValues();	
		parameters.put(KEY_WORD, word);
		return parameters;	
	}

	public long addFilter(
			String word){
		
		ContentValues parameters = initParameters(word.trim());
		//
		return this.database.insert(TABLE_FILTER, null, parameters);
	}

	public boolean updateFilter(
			int id,
			String word){
		ContentValues parameters = initParameters(word.trim());
		//update function return a number of row affected
		return this.database.update(TABLE_FILTER, parameters, KEY_ID + " = " + id, null) > 0;
	}
	
	public boolean updateAdvanced(ContentValues parameters, String whereClause, String[] whereArgs){
		
		return this.database.update(TABLE_FILTER, parameters, whereClause, whereArgs) > 0;
	}

	public boolean deleteFilter(int _id){
		return this.database.delete(TABLE_FILTER, KEY_ID + " = " + _id, null) > 0;
	}
	public boolean deleteAll(){
		return this.database.delete(TABLE_FILTER, null, null) > 0;
	}
	public boolean deleteFilterByWord(String word){
		return this.database.delete(TABLE_FILTER, KEY_WORD + " = '" + word +"'", null) > 0;
	}
	
	public boolean isWordExist(String word){
		boolean isExist = false;
		Cursor cr = selectFilterByWord(word);
		if(cr.getCount() > 0){
			isExist = true;
		}
		cr.close();
		return isExist;
	}
}